package own.stu.jobgib.playown.vediodown.bili.service;

import com.google.common.reflect.TypeToken;
import jodd.util.StringUtil;
import org.asynchttpclient.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import own.stu.jobgib.playown.vediodown.bili.model.seriesDetail.ArchivesItem;
import own.stu.jobgib.playown.vediodown.bili.model.seriesDetail.SeriesDetail;
import own.stu.jobgib.playown.vediodown.common.BaseModel;
import own.stu.jobgib.playown.vediodown.common.config.DefaultConfig;
import own.stu.jobgib.playown.vediodown.common.service.BaseDownloadService;
import own.stu.jobgib.playown.vediodown.common.service.CommonService;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

public class DownloadService extends BaseDownloadService {

    private static Logger logger = LoggerFactory.getLogger(DownloadService.class);

    private CommonService commonService;

    public DownloadService(DefaultConfig cfg, Long userId) {
        super(cfg, path + userId);
        commonService = new CommonService(cfg);
    }

    static {
        // TODO need repair
        blackLislMap.putIfAbsent(3493140465978044L, new ArrayList<>());

        FileFilter prefixFilter = file -> file.getName().endsWith(".mp4");

        File file = new File("/Users/qim/Desktop/vedio");
        if (file.isDirectory()) {
            for (File f : file.listFiles(prefixFilter)) {
                blackLislMap.get(3493140465978044L).add(f.getName());
            }
        }
    }

    private static List<String> blackListByUserId(Long userId) {
        return blackLislMap.get(userId);
    }

    public boolean downLoadSeriesDetail(String url) throws Exception {

        /*
        // TODO
        ParameterizedTypeReference<BaseModel<SeriesDetail>> typeReference = new ParameterizedTypeReference<BaseModel<SeriesDetail>>() {
        };

        MediaInfoService infoService = new MediaInfoService();
        BaseModel<SeriesDetail> record = infoService.getRecord(url, typeReference);*/

        // 泛型参数  参考
        //RestTemplate template = new RestTemplate();
        //ResponseEntity<BaseModel<Integer>> xxx = template.getForEntity("xxx", BaseModel.class);

        Future<Response> f = client.prepareGet(url).execute();
        Response response = f.get();
        String responseBody = response.getResponseBody();
        BaseModel<SeriesDetail> record = gson.fromJson(responseBody, new TypeToken<BaseModel<SeriesDetail>>() {
        }.getType());

        if (record == null || record.getData() == null) {
            return false;
        }

        SeriesDetail data = record.getData();
        List<ArchivesItem> archives = data.getArchives();
        archives.forEach(v -> {
            process("yt-dlp",
                    "-P ",
                    "/Users/qim/Desktop/eng",
                    "--cookies-from-browser", "chrome:" + getCookies_path(),
                    SHORT_LINK_URL + v.getBvid());
        });

        return true;
    }

    public static void main_1(String[] args) throws Exception {
        DefaultConfig cfg = new DefaultConfig();
        DownloadService service = new DownloadService(cfg, 382223675L);
        /*SimpleMediaItem item = new SimpleMediaItem();
        item.setBvId("BV1bo4y1Y7fw");
        item.setCover("http://i0.hdslb.com/bfs/archive/ddd7c0ef31d55305fd21959f6f6ae22279bd6bc4.jpg");
        item.setId(1L);
        item.setTitle("其实妈妈并不喜欢吃野菜 只是想多陪陪外婆而已");
        service.process(item);*/

        service.downLoadSeriesDetail("https://api.bilibili.com/x/series/archives?mid=382223675&series_id=2605678&only_normal=true&sort=desc&pn=1&ps=60&current_mid=0");

        cfg.destory();
    }
}
